package com.pjnkj.thunder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjnkj.thunder.common.json.Body;
import com.pjnkj.thunder.common.json.Meta;
import com.pjnkj.thunder.entity.Effect;
import com.pjnkj.thunder.service.EffectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.pjnkj.thunder.common.json.Meta.CODE_470;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pjnkj
 * @since 2022-03-28
 */
@RestController
@RequestMapping("/thunder/effect")
public class EffectController {
    @Autowired
    EffectService effectService;

    /**
     * 删除数据
     *
     * @param effect
     */
    @PostMapping("delete")
    public Body delete(Integer id) {
        Boolean flag = effectService.removeById(id);
        if (flag == true) {
            return Body.newInstance("删除成功");
        }
        return Body.newInstance(Meta.CODE_400, "删除失败");
    }

    /**
     * 增加数据
     *
     * @param effect
     */
    @PostMapping("create")
    public Body create(@Validated(Effect.class) @RequestBody Effect effect) {
        Boolean flag = effectService.saveOrUpdate(effect);
        if (flag == true) {
            return Body.newInstance("保存成功");
        }
        return Body.newInstance(Meta.CODE_400, "保存失败");
    }

    /**
     * 修改数据
     *
     * @param effect
     */
    @PostMapping("update")
    public Body update(@Validated(Effect.class) @RequestBody Effect effect) {
        Boolean flag = effectService.updateById(effect);
        if (flag == true) {
            return Body.newInstance("修改成功");
        }
        return Body.newInstance(Meta.CODE_400, "修改失败");
    }

    /**
     * 查询数据
     *
     * @param
     */
    @PostMapping("find")
    public Body find(String id) {
        Effect info = effectService.getById(id);
        if (info != null) {
            return Body.newInstance(info);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
     * 查询数据list
     *
     * @param
     */
    @PostMapping("findlist")
    public Body findlist() {
        List<Effect> list = effectService.list();
        if (list != null && list.size() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }

    /**
     * 分页查询数据
     *
     * @param
     */
    @PostMapping("page")
    public Body page(Integer current, Integer size) {
        Page<Effect> pg = new Page<Effect>(current, size);
        IPage<Effect> list = effectService.page(pg);
        if (list != null && list.getSize() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }
}
